<?php

namespace App\Http\Middleware;

use App\Enum\TravelEnum;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Log;

class ApiCheck
{
    private $route = [
        'api/v1/login',
        'api/v1/generate/token',
        'api/v1/sms/code',
        'api/v1/wx/login',
        'api/v1/wx/secrettoken', //发布问题
        'api/v1/wx/secret', //发布问题
        'api/v1/driver/register',
        'api/v1/driver/my/account/modify/password',
        'api/v1/get/time',
        'api/v1/qrcode/driver/info',
        'api/v1/qrcode/villager/info',
        'api/v1/qrcode/volunteer/info',
        'api/v1/qrcode/my/volunteer/info',
        'api/v1/qrcode/order/info',
        'api/v1/qrcode/activity/order/info',
        'api/v1/qrcode/activity/order/ord',
    ];
    /**
     * Handle an incoming request.
     *
     * @param Request $request
     * @param Closure $next
     * @return JsonResponse|mixed|null
     */
    public function handle(Request $request, Closure $next)
    {
        $url = $request->path();

        $platformId = 0; $platformName = '-';
        if (!in_array($url, $this->route)) {
            //平台标识id
            $platformId = $request->header('App-Platform-Id', $platformId);
            if (empty($platformId)) {return fail(10010, '平台参数为空');}

            //平台标识
            $appIdKeys = array_keys(TravelEnum::APP_PLATFORM_ID);
            if (!in_array($platformId, $appIdKeys)) {return fail(10010, '平台参数错误');}

            $platformName = TravelEnum::APP_PLATFORM_ID[$platformId];
            $request->appId = $platformId;
            $request->appPlatform = $platformName;
        }
        Log::notice(sprintf("【check.api】 user=%-5d platform_name=%-10s url=>%s", $request->userId,$platformName, $url));

        return $next($request);
    }
}

